package com.sec.android.app.myfiles.module.suggestion;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

public class SuggestionAdapterImp extends AbsFileListAdapterImp {

    private boolean isEnabled = true;

    public SuggestionAdapterImp(Context context, NavigationInfo navigationInfo, int layout, Cursor c, int flags) {
        super(context, navigationInfo, layout, c, flags);
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        View suggestionView = v.findViewById(R.id.home_list_item_container);
        if (suggestionView != null) {
            suggestionView.setBackground(null);
        }
        super._bindView(context, vh, v, record, position);
    }

    @Override
    protected void _bindDefaultInfoView(Context context, ListViewHolder vh, FileRecord record) {
        TextView textName = vh.getView(FileListViewHolder.NAME, TextView.class);
        if (textName != null) {
            textName.setText(record.getName());
            textName.setEllipsize(TextUtils.TruncateAt.MIDDLE);
        }
        TextView sizeView = vh.getView(FileListViewHolder.SIZE, TextView.class);
        if (sizeView != null) {
            sizeView.setVisibility(View.GONE);
        }
    }

    @Override
    protected boolean _bindThumbnailView(Context context, ListViewHolder vh, View v, FileRecord record) {
        if (record instanceof SuggestionFileRecord) {
            if (vh.getView(FileListViewHolder.FILE_TYPE_ICON) != null) {
                int iconResId = -1;
                switch (((SuggestionFileRecord) record).getRealStorageType()) {
                    case Local:
                        iconResId = R.drawable.home_list_item_ic_folder;
                        break;
                    default:
                        break;
                }
                if (iconResId != -1) {
                    ImageView iconView = vh.getView(FileListViewHolder.FILE_TYPE_ICON, ImageView.class);
                    if (iconView != null) {
                        iconView.setImageResource(iconResId);
                    }
                }
            }
        }
        return false;
    }

    @Override
    protected void _bindContentDescription(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        if (!getEnabled()) {
            v.setContentDescription(context.getResources().getString(R.string.folder) + ", " + record.getName() + ", "
                    + context.getResources().getString(R.string.tts_dimmed));
        } else {
            super._bindContentDescription(context, vh, v, record, position);
        }
    }

    @Override
    public String getSelection() {
        String where = null;
        boolean showHidden = PreferenceUtils.getShowHiddenFiles(mContext);
        if (!showHidden) {
            StringBuilder sb = new StringBuilder();
            String path = mTableInfo.getColumnName(COLUMN_ID.PATH);
            String name = mTableInfo.getColumnName(COLUMN_ID.NAME);

            sb.append(" ((LOWER(").append(path).append(") NOT LIKE '%/.%')  AND NOT(").append(name).append(" LIKE '.%'))");
            if (sb.length() > 0) {
                where = sb.toString();
            }
        }
        return where;
    }

    @Override
    public String[] getSelectionArgs() {
        return null;
    }

    @Override
    public String getOrderBy() {
        return mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.NAME) + " ASC";
    }

    @Override
    protected FileRecord _getFileRecord(Cursor cursor) {
        FileRecord ret = null;
        final int pathIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PATH);
        final int nameIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.NAME);
        final int storageTypeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.STORAGE_TYPE);

        if (cursor != null) {
            final String path = cursor.getString(pathIndex);
            final String name = cursor.getString(nameIndex);
            final int storageTypeValue = cursor.getInt(storageTypeIndex);
            StorageType storageType = FileRecord.StorageType.values()[storageTypeValue];
            ret = new SuggestionFileRecord(storageType, path, name);
        }
        return ret;
    }

    @Override
    public String getFilePath(Cursor cursor) {
        return getFilePath(cursor, mTableInfo.getIndex(COLUMN_ID.PATH));
    }

    @Override
    public StorageType getStorageType() {
        return StorageType.Suggestion;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    public boolean getEnabled() {
        return isEnabled;
    }

    @Override
    protected ListViewHolder _createViewHolder(View v) {
        ListViewHolder vh = ListViewHolder.createHolder(FileListViewHolder.MAX);
        vh.addView(FileListViewHolder.NAME, v.findViewById(R.id.home_list_item_text));
        vh.addView(FileListViewHolder.SIZE, v.findViewById(R.id.home_list_item_text_second));
        vh.addView(FileListViewHolder.FILE_TYPE_ICON, v.findViewById(R.id.home_list_item_thumbnail));
        v.setFocusable(false);
        return vh;
    }
}
